package com.wlxk.crawler.core.manager.qishuta;

import cn.hutool.core.util.URLUtil;
import com.wlxk.crawler.core.util.HttpClientDownloader;
import org.springframework.util.CollectionUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.net.URL;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description:
 * @author: 21054523
 * @time: 2023/11/16 15:54
 */
public class QishutaPageProcessor implements PageProcessor {

    private Site site = Site.me()
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")
            .setRetryTimes(3).setSleepTime(1000).setTimeOut(10000);


    @Override
    public void process(Page page) {

        String url = page.getUrl().toString();
        page.putField("url", page.getUrl().toString());
        if (url.contains("/Shtml")) {
            page.putField("pageType", "home");
            String[] split = url.split("/");
            String articleId = split[split.length - 1].replace("Shtml", "").replace(".html", "");
            page.putField("articleId", articleId);
            String title = page.getHtml().xpath("//div[@class='cataloginfo']/h3/text()").all().get(0);
            page.putField("title", title);
            List<String> list = page.getHtml().xpath("//div[@class='infotype']/p/text()").all();
            for (String s : list) {
                if (s.contains("作者")) {
                    page.putField("author", s.replace("作者：", ""));
                }
            }
            URL uri = URLUtil.url(url);
            List<String> all = page.getHtml().xpath("//select[@name='pageselect']").$("option", "value").all()
                    .stream().map(item -> uri.getProtocol() + "://" + uri.getHost() + item).toList();
            page.addTargetRequests(all);
        } else {
            List<String> all1 = page.getHtml().xpath("//div[@class='cataloginfo']/h3/text()").all();
            if (!CollectionUtils.isEmpty(all1)) {
                page.putField("pageType", "list");
                String[] split = url.split("/");
                String s = split[split.length - 1];
                if (s.contains(".html")) {
                    page.putField("articleId", split[split.length - 2]);
                } else {
                    page.putField("articleId", s);
                }
                List<Selectable> nodes = page.getHtml().xpath("//div[@class='list_xm']").nodes();
                Selectable selectable = nodes.get(nodes.size() - 1);
                List<String> links = selectable.xpath("//ul/li").links().all();
                List<String> texts = selectable.xpath("//ul/li/a/text()").all();
                page.putField("urlList", links);
                page.putField("textList", texts);
            } else {
                page.putField("pageType", "chapter");
                String context = page.getHtml().xpath("//div[@id='novelcontent']/tidyText()").all().get(0);
                context = contextAmend(page, context);
                page.putField("content", context);
                String[] split = page.getUrl().toString().split("/");
                page.putField("articleId", split[split.length - 2]);
                String[] split1 = split[split.length - 1].replace(".html", "").split("_");
                page.putField("chapterId", split1[0]);

                List<Selectable> nodes = page.getHtml().xpath("//ul[@class='novelbutton']/li/p[@class='p3']").nodes();
                if (!CollectionUtils.isEmpty(nodes)) {
                    String text = nodes.get(0).xpath("//a/text()").all().get(0);
                    if ("下一页".equals(text)) {
                        page.addTargetRequests(nodes.get(0).links().all());
                    }
                }
            }
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public String contextAmend(Page page, String context) {

        String zhangJie = "";
        String fenYe = "";

        Matcher zjMt = Pattern.compile("第(.*?)章(.*?)\\((.*?)\\)").matcher(context);
        while (zjMt.find()) {
            zhangJie = zjMt.group();
        }

        Matcher fyMt = Pattern.compile("\\((.*?)\\)").matcher(zhangJie);
        while (fyMt.find()) {
            fenYe = fyMt.group();
        }
        String zhangJieName = zhangJie.replace(fenYe, "");

//        if (fenYe.contains("(第1/")) {
//            context = context.replace(zhangJie, zhangJieName);
//        } else {
            context = context.replace(zhangJie, "");
//        }

        page.putField("chapterTitle", zhangJieName);

        Matcher matcher1 = Pattern.compile("<(.*?)>").matcher(context);
        while (matcher1.find()) {
            String text = matcher1.group();
            context = context.replace(text, "");
        }
        context = context.replaceAll("     \n", "  ");
        context = context.replaceAll("    ", "  ");
        context = context.replaceAll("  ", "");
        context = context.replace("最新网址：m.qishuta.org\n", "");
        context = context.replace("（本章未完，请点击下一页继续阅读）\n\n\n", "");
        context = context.replace("* \n", "");
        context = context.replace("上一章 ", "");
        context = context.replace("返回目录 ", "");
        context = context.replace("加入书签 ", "");
        context = context.replace(" 上一页 \n", "");
        context = context.replace(" 下一页 \n", "");
        context = context.replace(" 下一章 \n", "");

        context = context.replaceAll("\n  \n", "");
        context = context.replaceAll("\n \n\n \n\n \n\n", "");
        context = context.replaceAll("\n \n", "");
        context = context.replaceAll("\n\n\n", "");
        context = context.replaceAll("\n  第", "  第");
        context = context.replaceAll("\n  \n\n", "");
        return context;
    }


    public static void main(String[] args) {
        Spider.create(new QishutaPageProcessor()).addPipeline(new QishutaPipeline()).setDownloader(new HttpClientDownloader())
                .addUrl("https://m.qishuta.org/Shtml117151.html")
//                .addUrl("https://m.qishuta.org/du/117/117151/38841983.html")
//                .addUrl("https://m.qishuta.org/du/117/117151/38841983_2.html")
//                .addUrl("https://m.qishuta.org/du/117/117151/38841983_3.html")
                .thread(1).run();
    }


}
